﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Module.Interfaces.Model;
using umbraco.cms.presentation.Trees;
using umbraco.interfaces;

namespace Module.PageComments.Manager
{
    public class CommentsManager : BaseTree, IApplicationTree
    {
        private const string _applicationName = "ism-modules";
        public string Alias
        {
            get { return "Page Comments"; }
        }

        public string Application
        {
            get { return _applicationName; }
        }

        public string Icon
        {
            get { return "comments.png" ; }
        }

        public string IconOpen
        {
            get { return "comments.png"; }
        }

        public byte OrderNr
        {
            get { return 255;}
        }

        public CommentsManager() : base(_applicationName) { }
        public CommentsManager(string application): base(application){}
        
        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.NodeType = "init" + this.TreeAlias;
            rootNode.NodeID = "init";
            // Statistic cs=Items;
            rootNode.Text = this.Alias;
            //RootNode.Text = "Manage Comments - (new:"+cs.curpage.ToString()+"; total:" +cs.total.ToString()+")";
            //if (cs.curpage > 0) rootNode.Style.HighlightNode(); else RootNode.Style.DimNode();
            //rootNode.Icon = "blogs.png";
            //rootNode.OpenIcon = "blogs.png";

            rootNode.Action = "javascript:openComents()";
        }
        protected override void CreateAllowedActions(ref List<IAction> actions)
        {
            actions.Clear();
            //actions.Add(ActionNew.Instance);
            //actions.Add(ActionDelete.Instance);
            //actions.Add(ContextMenuSeperator.Instance);
            //actions.Add(ActionRefresh.Instance);
        }

        public override void Render(ref XmlTree tree)
        {
            if (string.IsNullOrEmpty(base.NodeKey) || base.NodeKey.Equals("init"))
            {

                //foreach (Statistic.PerPage item in Items.perpage)
                //{
                //    XmlTreeNode node = XmlTreeNode.Create(this);
                //    node.NodeID = item.id.ToString();
                //    Content cnode = new Content(item.id);
                //    node.Text = cnode.Text + (item.total > 0 ? "  (" + item.total.ToString() + " -new) " : "");
                //    node.Action = string.Format("javascript:openComentsForItem({0});", item.id);
                //    node.Icon = cnode.ContentTypeIcon;
                //    if (item.total > 0) node.Style.HighlightNode(); else node.Style.DimNode();
                //    node.OpenIcon = "settingDataType.gif";
                //    node.NodeType = "CommentsForPage";
                //    node.Source = base.GetTreeServiceUrl(item.id);
                //    node.HasChildren = false;
                //    this.OnBeforeNodeRender(ref tree, ref node, EventArgs.Empty);
                //    if (node != null)
                //    {
                //        tree.Add(node);
                //        this.OnAfterNodeRender(ref tree, ref node, EventArgs.Empty);
                //    }
                //}
            }
        }
        public override void RenderJS(ref StringBuilder Javascript)
        {
            Javascript.Append("\r\n\t\t\tfunction openComentsForItem(id) {\r\n\t\t\t\tUmbClientMgr.contentFrame('ism/editcomments.aspx?pageid=' + id);\r\n\t\t\t}\r\n\t\t\t");
            Javascript.Append("\r\n\t\t\tfunction openComents() {\r\n\t\t\t\tUmbClientMgr.contentFrame('ism/editcomments.aspx');\r\n\t\t\t}\r\n\t\t\t");
    
        }
        //private Statistic GetStatisticInfo()
        //{
        //    Statistic result = new Statistic();
        //    result.pagesize = 0;
        //    result.curpage = 0;
        //    result.start = DateTime.Now;
        //    string tsql = "select count(*) from comment";
        //    ISqlHelper sqlHelper = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);
        //    result.total = sqlHelper.ExecuteScalar<int>(tsql,new IParameter[]{});
        //    result.curpage = sqlHelper.ExecuteScalar<int>(tsql+" where (spam=1)", new IParameter[] { });
        //    tsql = string.Format("select a.amount,c.tamount, n.[id],n.[text],n.[level],n.[path] ,n.* from (select count(*) tamount, nodeid from comment where id>0 group by nodeid)c left join (select count(*) amount, nodeid from comment where id>0 and spam=1 group by nodeid)a on c.nodeid=a.nodeid inner join umbraconode n on n.id=c.nodeid");
        //    IRecordsReader recReader = sqlHelper.ExecuteReader(tsql, new IParameter[] { });
        //    while (recReader.Read())
        //    {
        //        Statistic.PerPage r = new Statistic.PerPage();
        //        r.id = recReader.GetInt("id");
        //        r.name = recReader.GetString("text");
        //        r.total = recReader.GetInt("amount");
        //        result.perpage.Add(r);
        //    }
        //    //calculate amount of pages
        //    double pSize = result.pagesize;
        //    double pTtl = result.total;
        //    result.pages = (int)Math.Ceiling(pTtl / pSize);

        //    result.end = DateTime.Now;
        //    result.duration = (int)Math.Ceiling(DateTime.Now.Subtract(result.start).TotalMilliseconds);
        //    return result;
        //}
    }
}
